package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.Record;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.event.SoundEvent;
	import engine.flag.ResourcesPath;
	import engine.logic.LogSendLogic;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.BaseObject;
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.tab.NxTabEvent;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;

	/**
	 * 根骨界面逻辑
	 * @author zhouqiang
	 * 
	 */	
	public class IntensifyLogic
	{
		private static var _self :IntensifyLogic;
		
		private var _ui:IntensifyUI;
		
		private var _model:ModelLocator;
		
		private var _bind:BindManage;
		
		/**主角绑定index*/
		private var _bindIndex:int	=	-1;
		
		/**伙伴绑定index*/
		private var _bindPartnerIndex:int	=	-1;
		
		/**背包绑定index*/
		private var  _bindPack:int	=	-1;
		
		
		/**伙伴绑定index*/
		private var  _bindFlag:int	=	-1;
		
		/**标签index*/
		private var _tabIndex:int	=	0;
		
		/**所有Group数组*/
		private var _allInfoArr:Array;	
		
		/**丹药服用状态数组*/
		private var isUseArr:Array;
		
		/**背包里丹药的种类和个数*/
		private var medicineArr:Array;
		
		/**所有丹药ID*/
		private var allArr:Array;
		
		/**TIPs数组*/
		private var _tipsArr:Array;
		
		/**伙伴的configID*/
		private var _partner:String;
		/**伙伴等级**/
		private var _partnerLevel:int;
		
		/**伙伴的名字*/
		private var _partnerNAME:String;
		
		/**所选角色的view*/
		private var _partnerView:ObjectData;
		
		/**3种丹*/
		private var planArr:Array;
		private var planArrIndex:Array;
		
		/**服用丹增加的属性总值*/
		private var resultArr:Array;
		private var resultArrIndex:Array;
		
		/***/
		private var strIDArr:Array;
		
		/**背包是否有可以使用的丹药*/
		private var _isCanUse:Boolean	=	false;
		
		/**伙伴个数*/
		private var _partnerNum:int;
		
		private var typeStrArr:Array;
		/**引导**/
		private var guildMc:MovieClip;
		public static function getInstance():IntensifyLogic
		{
			if( _self == null)
			{
				_self = new IntensifyLogic();
			}
			return _self;
		}	
		public function IntensifyLogic()
		{
		}
		
		public function Init():void
		{
			_model 		 = 	ModelLocator.getInstance();
			_bind		 = 	BindManage.getInstance();
			_ui	  		 =	IntensifyUI.getInstance();
			_allInfoArr	 =	IntensifyUI.groupArr as Array;
			medicineArr	 = 	[];
			allArr		 =	[[34010100,34010200,34010300,34010400,34010500,34010600],
							 [34020100,34020200,34020300,34020400,34020500,34020600],
							 [34030100,34030200,34030300,34030400,34030500,34030600],];
			//锻筋丹,铸骨丹 炼皮丹
			planArr		 =	["MusclePercent","BonePercent","SkinPercent"];
			//服用锻筋丹增加的属性总值,服用铸骨丹增加的属性总值,服用炼皮丹增加的属性总值
			resultArr	 =	["MuscleTotal","BoneTotal","SkinTotal"];
			planArrIndex =	[-1,-1,-1];
			resultArrIndex= [-1,-1,-1];
			strIDArr	 =	["IntensifyUIStr_5","IntensifyUIStr_6","IntensifyUIStr_7"];
			//普攻，普防，
			typeStrArr	 =	["ImportStr_13","ImportStr_14","ImportStr_15"];
			_tipsArr	 =	["UIString_1000032","UIString_1000033","UIString_1000034"];	
			if( _bindFlag == -1 )
			{
				_bindFlag		=	_bind.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
				
			}
			InitGroup();		
		}
		
		/**
		 *初始化界面 
		 * 
		 */
		private function InitGroup():void
		{
			//页签
			addTab();		
			var img 	: NxClip;
			var itemMC 	: MovieClip;
			var btn		: MovieClip;
			var btnvip	: MovieClip;
			var obj		: Object	= new Object();
			obj["weakTime"]	=	-1;
			for( var i:int = 0 ; i <  _allInfoArr.length; i++ )
			{
				var tempNum:int	=	(_allInfoArr[i] as NxViewGroup).row
				for(var j:int = 0; j <  tempNum; j++)
				{
					itemMC			=	(_allInfoArr[i] as NxViewGroup).getItemByIndex(j);
					img				=	new NxClip();
					img.defaultSkin = 	"NotFound6";
					img.isCache		= 	true;
//					img.name		=	"img";		
					img.x		    = 	0;		
					img.y		    = 	0;
					itemMC["img"]	=	img;
					itemMC["img2"].addChild( img );
					
					btn				=	ResManager.newResClass("Castle_Item_0021") as MovieClip;
					btn.x			=	0;
					btn.y			=	0;
					btn.visible		=	false;
					itemMC["btn"]	=	btn;
					itemMC["btn"].buttonMode = true;
					itemMC["btn"]["btnType"] = 1;
					(itemMC["btn"]["T2"] as TextField).mouseEnabled = false;
					btn.gotoAndStop(1);
					itemMC["jia"].addChild( btn );
					btnvip				=	ResManager.newResClass("Castle_Item_0022") as MovieClip;
					btnvip.x			=	0;
					btnvip.y			=	0;
					btnvip.visible		=	false;
					itemMC["btnvip"]	=	btnvip;
					itemMC["btnvip"].buttonMode = true;
					itemMC["btnvip"]["btnType"] = 2;
					(itemMC["btnvip"]["T1"] as TextField).visible	= false;
					(itemMC["btnvip"]["T1"] as TextField).mouseEnabled = false;
					btnvip.gotoAndStop(1);
					itemMC["jia"].addChild( btnvip );
					itemMC.data		=	obj;
//					itemMC.buttonMode	=	true;
					itemMC.addEventListener(MouseEvent.ROLL_OVER,onMouseOverHandle);
					itemMC.addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
				}		
			}
		}
		
		/**
		 * TIps
		 * @param e
		 * 
		 */		
		private function onMouseOverHandle(e:MouseEvent):void
		{
			var val:Object		=	e.currentTarget.data;
			if(val["weakTime"] == -1)return;
			TipsMainLogic.instance.showMain(val);
		}
		
		private function onMouseOutHandle(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			if(target.totalFrames > 1){
				target.gotoAndStop(1);
			}
			TipsMainLogic.instance.hide();
		}
		
		private function OnUseBtnOverHandle(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			target.gotoAndStop(2);
			var configID:String	=	target.data;
			var nameStr:String	=	StaticIni.getSwfLangStr(configID);
			var obj:Object	=	StaticIni.getIniObj(configID);
			var str:String	=	StaticIni.getSwfLangStrVar("IntensifyUIStr_8",[nameStr])
			if(target["btnType"] == 2){
				str = StaticIni.getSwfLangStrVar("IntensifyUIStr_10",[nameStr, obj["DirectlyUseItemValue"]])
			}
			TipsMainLogic.instance.showString(str);
		}
		private function addTab():void
		{
			_ui.listTab.addItem(StaticIni.getSwfLangStr("ImportStr_13"));
			_ui.listTab.addItem(StaticIni.getSwfLangStr("ImportStr_14"));
			_ui.listTab.addItem(StaticIni.getSwfLangStr("ImportStr_15"));			
		}
		
		/**
		 * 事件侦听
		 * 
		 */		
		private function addListener():void
		{
			_ui.listTab.addEventListener( NxTabEvent.TAB_CLICK, onTabClickHandle);
			_ui.windowBG.addEventListener( NxWindowEvent.WINDOW_CLOSE,	onClickClose);
//			_ui.resultLab.addEventListener(MouseEvent.ROLL_OVER,onTextOverHandle);
//			_ui.resultLab.addEventListener(MouseEvent.ROLL_OUT,onTextOutHandle);
			_ui.windowBG.nxWindowhelpBtn			.addEventListener(MouseEvent.MOUSE_OVER,OverhelpHandler);
			_ui.windowBG.nxWindowhelpBtn			.addEventListener(MouseEvent.MOUSE_OUT,OuthelpHandler);

		}
		
		private function onTextOverHandle(e:MouseEvent):void
		{
			var str:String	=	StaticIni.getSwfLangStr(_tipsArr[_tabIndex]);
			TipsMainLogic.instance.showString(str);
		}
		
		private function onTextOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 * 
		 * @param e
		 * 
		 */
		private function OverhelpHandler(e:MouseEvent):void
		{
			var tip:String = StaticIni.getSwfLangStr("UIStr_1001900"); 
			TipsMainLogic.instance.showString(tip);
		}
		private function OuthelpHandler(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		public function BeforeShow():void
		{
			isUseArr				=	[[],[],[]];
			_ui.planLab.visible		=	false;
			_ui.windowBG.helpBtnVisible = true ; 
			if(_ui.imgBG.source == null)
			{
				SwfLoader.StartLoad(StageInfo.swfResPath + "IntensifyBG.swf",null,loadChessboardFun);
			}
			else
			{
				clearGroupInfo();
				addListener();
				unBind();	
				groupInfo();
			}	
		}
		
		private function loadChessboardFun(mc:MovieClip,pL:Array):void
		{
			_ui.imgBG.source	=	mc;
			clearGroupInfo();
			addListener();
			unBind();	
			groupInfo();
		}
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_partnerNum++;
		}
		
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			
		}
		private function OnAddPackEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			addPackItem(ObjID,Obj,data);
		}
		private function OnRemovePackEquip(ObjID:String,Obj:BaseObject,data:*):void
		{
			removePackItem(ObjID,Obj,data);
		}
		
//		/**
//		 * 使用丹药成功
//		 */	
//		public function onaddPartenHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
//		{
//			var Str:String	=	PropertiesName;
//		}
		
		
		public function AfterHide():void
		{
			//nBind();
			for (var i:int = 0; i < planArrIndex.length; i++) 
			{
				planArrIndex[i]	 = _bind.UnbindProp(_partnerView,planArr[i],planArrIndex[i])
				resultArrIndex[i]= _bind.UnbindProp(_partnerView,resultArr[i],resultArrIndex[i])	
			}
			if(_bindIndex != -1)
			{
				_bindIndex	=	_bind.UnbindRecord("PillUseRec",_bindIndex);
			}
			if(_bindIndex != -1)
			{
				_bindPartnerIndex	= _bind.UnbindRecord("PartnerPillUseRec",_bindPartnerIndex);
			}
			_ui.listTab.removeEventListener(NxTabEvent.TAB_CLICK,onTabClickHandle);
			_ui.windowBG	.removeEventListener(	NxWindowEvent.WINDOW_CLOSE,	onClickClose);
			
//			_ui.resultLab.removeEventListener(MouseEvent.ROLL_OVER,onTextOverHandle);
//			_ui.resultLab.removeEventListener(MouseEvent.ROLL_OUT,onTextOutHandle);
			_bindPack	=	_bind.UnbindView(View.VIEWPORT_TOOL, _bindPack);
//			if( _bindFlag != -1 )
//			{
//				_bindFlag		=	_bind.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
//				
//			}
			
			if(guildMc != null){
				_ui.removeChild(guildMc);
				guildMc = null;
			}
		}
		/**
		 * 解除绑定 
		 * 从新绑定
		 */		
		private function unBind():void
		{
			if(_bindIndex != -1)
			{
				_bindIndex	=	_bind.UnbindRecord("PillUseRec",_bindIndex);
			}
			if(_bindPartnerIndex != -1)
			{
				_bindPartnerIndex	= _bind.UnbindRecord("PartnerPillUseRec",_bindPartnerIndex);
			}
			_bindPartnerIndex		=	-1;
			_bindIndex				=	-1;
			if(_partner == "")
			{
				_bindIndex	=	_bind.BindRecord(_model.scene,"PillUseRec",onaddPerHandle,onremovePerHandle);
			}
			else
			{
				_bindPartnerIndex	=	_bind.BindRecord(_model.scene,"PartnerPillUseRec",onaddPerHandle,onremovePerHandle)
			}
			medicineArr = [];
			_bindPack		=	_bind.UnbindView(View.VIEWPORT_TOOL, _bindPack);
			if(_bindPack ==-1)
			{
				_bindPack			=	_bind.BindView(_model.scene, View.VIEWPORT_TOOL, OnAddPackEquip, OnRemovePackEquip, null, removePackItem);
			}		
			//planArrIndex[_tabIndex]	  = _bind.BindProp(_partnerView,planArr[_tabIndex],this,"setPlan");
			resultArrIndex[_tabIndex] = _bind.BindProp(_partnerView,resultArr[_tabIndex],this,"setResult")
			_ui.windowBG.title	=	StaticIni.getSwfLangStr(_partnerView.GetProperties("ConfigID"));	
		}
		public function set setPlan(str:String):void
		{
			if(int(str) > 100)
			{
				str = "100";
			}
			_ui.planLab.visible		=	false;
		//	_ui.planLab.htmlText	=	StaticIni.getSwfLangStrVar("IntensifyUIStr_4",[str]);
		}
		public function set setResult(str:String):void
		{
			var _arr:Array = str.split(",");
			var num1:int;
			var num2:int;
			if(_arr.length == 1){
				num1 = int(str);
				_ui.resultLab.htmlText	=	StaticIni.getSwfLangStrVar(strIDArr[_tabIndex],[num1]);
			} else {
				num1 = _arr[0] == "" ? 0 : _arr[0];
				num2 = _arr[1] == "" ? 0 : _arr[1];
				_ui.resultLab.htmlText	=	StaticIni.getSwfLangStrVar(strIDArr[_tabIndex],[num1, num2]);
			}
		}
		
		/**
		 *tab页签点击事件 
		 * 
		 */		
		private function onTabClickHandle(e:NxTabEvent):void
		{
			if( e != null )
			{
				_tabIndex		=	e.item.index;
			}
			
			for (var i:int = 0; i < planArrIndex.length; i++) 
			{
				planArrIndex[i]	 = _bind.UnbindProp(_partnerView,planArr[i],planArrIndex[i])
				resultArrIndex[i]= _bind.UnbindProp(_partnerView,resultArr[i],resultArrIndex[i])	
			}
//			if(planArrIndex[_tabIndex] == -1)
//			{
//				_bind.BindProp(_partnerView,planArr[_tabIndex],this,"setPlan");
//			}
			
			if(resultArrIndex[_tabIndex] == -1)
			{
				_bind.BindProp(_partnerView,resultArr[_tabIndex],this,"setResult")
			}		
			clearGroupInfo();
			groupInfo();
		}
		
		/**
		 * 使用丹药成功
		 */	
		public function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			clearGroupInfo();
			var str:String;	
			var rec:Record;
			var arr:Array;
			var tempArr:Array;
			if(_partner == "")
			{
				rec	=	_model.scene.GetProperties("PillUseRec");
				tempArr =	rec.recArr;	
			}
			else
			{
				rec	=	_model.scene.GetProperties("PartnerPillUseRec");
				arr =	rec.recArr;	
				//取出所点击的伙伴的数据
				if(arr.length > 0)tempArr=getPartnerInfo(arr);
			}

			isUseArr		=	tempArr;
			groupInfo();
			
		}
		

		/**
		 * 从表中取出所选伙伴的数据
		 * @return 
		 * 
		 */		
		private function getPartnerInfo(arr:Array):Array
		{
			var tempArr:Array	=	[];
			for(var i:int = 0;i < arr.length;i++)
			{
				if(arr[i][3] == int(_partner))
				{
					tempArr.push(arr[i])
				}
			}
			return tempArr
		}
		/**
		 *在这里用不到 
		 */		
		public function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{			
		}
			
		/**
		 * 绑定背包检索是否有丹药
		 * 增加时调用
		 */
		public function addPackItem(ObjID:String,Obj:BaseObject,data:*):void
		{
			var str:String	=	Obj.GetProperties("ConfigID"); 
			for(var i:int = 0;i<allArr.length;i++ )
			{
				for(var j:int = 0;j < allArr[i].length;j++)
				{
					var _arr:Array = new Array;
					if (str	==	allArr[i][j])
					{
						var amount:int = ComposeItemLogic.getInstance().searchItem(int(str),1);
						_arr.push(str);
						_arr.push(amount);
						medicineArr.push(_arr);
					}
				}
			}
//			clearGroupInfo();
//			groupInfo();
		}
		/**
		 * 背包减少物品时调用
		 * @param objID
		 * @param Obj
		 * @param data
		 * 
		 */		
		public function removePackItem(objID:String,Obj:BaseObject,data:*):void
		{
			var str:String	=	Obj.GetProperties("ConfigID"); 
			for(var i:int = 0;i<medicineArr.length;i++ )
			{
				var amount:int = ComposeItemLogic.getInstance().searchItem(int(str),1);
				if(str == medicineArr[i][0] && amount == 0)
				{
					medicineArr.splice(i,1);
				} else if(str == medicineArr[i][0] && amount > 0){
					medicineArr[i][1]	= amount;
				}
			}
			clearGroupInfo();
			groupInfo();
		}
		/**
		 * 检测背包是否有对应的丹药，有返回 index，否返回-1
		 * @param ID
		 * @return 
		 * 
		 */		
		private function isHave(ID:int):int
		{
			for (var index:String in medicineArr)
			{
				if(ID == int(medicineArr[index][0]))return int(index);
			}
			return -1;
		}	
		
		
		/**
		 * 
		 * 处理数据并显示出来
		 */		
		private function groupInfo():void
		{
			var img 	: NxClip;
			var itemMC 	: MovieClip;
			var btn		:SimpleButton;
			var temp	:Array;	
			var obj		:Object;
			//丹药图片ID
			var photoID:String;
			//表格行数
			var count	:int;
			
			var vipLevel:int	=	_model.scene.Role.GetProperties("VIPLevel");
			
			_isCanUse	=	false;	
			if(vipLevel < 5)
			{
				count	=	0;
			}
			else
			{
				if(vipLevel <7)
				{
					count	=	5;
				}
				else
				{
					count	=	6;
				}
			}
			//丹药服用情况为0时
//			if(isUseArr == null)
//			{
				for(var k:int = 0 ; k <  count; k++)
				{
//					if(isHave(allArr[_tabIndex][k]))
//					{
						itemMC					=	(_allInfoArr[k] as NxViewGroup).getItemByIndex(0);
						itemMC["btn"].visible	=	false;
//						itemMC["btn"].addEventListener(MouseEvent.CLICK,onUseMedicineHandle);
//						itemMC["btn"].addEventListener(MouseEvent.ROLL_OVER,OnUseBtnOverHandle)
//						itemMC["btn"].addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
						itemMC["btnvip"].visible	=	true;
						itemMC["btnvip"].data		=	allArr[_tabIndex][k];
						itemMC["btnvip"].addEventListener(MouseEvent.CLICK,onUseMedicineHandle);
						itemMC["btnvip"].addEventListener(MouseEvent.ROLL_OVER,OnUseBtnOverHandle)
						itemMC["btnvip"].addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
//					}
				}
				//return;
//			}
				
			for( var i:int = 0; i <  6; i++ )
			{
				temp =	queryMedicine(allArr[_tabIndex][i]);
				var medicineInfo:int = isHave(allArr[_tabIndex][i]);
				if(temp.length < 1)
				{
					if((medicineInfo != -1) || i < count)
					{
						itemMC			=	(_allInfoArr[i] as NxViewGroup).getItemByIndex(0);
						//btn				=	itemMC["btn"] as SimpleButton;
						if(medicineInfo != -1){
							itemMC["btnvip"].visible	=	false;
							itemMC["btn"].visible		=	true;
							itemMC["btn"]["T2"].htmlText	= String(medicineArr[medicineInfo][1]);
							itemMC["btn"].data			=	allArr[_tabIndex][i];
							itemMC["btn"].addEventListener(MouseEvent.CLICK,onUseMedicineHandle);
							itemMC["btn"].addEventListener(MouseEvent.ROLL_OVER,OnUseBtnOverHandle)
							itemMC["btn"].addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);	
						}
					}
//					if(isHave(allArr[_tabIndex][i]))
//					{
//						_isCanUse	=	true;
//					}
				}
				else
				{
					var j:int = 0 ;
					var tempNum:int	=	temp[1];
					for( j; j <  tempNum; j++)
					{
						itemMC			=	(_allInfoArr[i] as NxViewGroup).getItemByIndex(j);
//						btn				=	itemMC["btn"] as SimpleButton;	
						itemMC["btn"].visible		=	false;
						itemMC["btnvip"].visible	=	false;
//						img				=	itemMC.getChildByName("img") as NxClip;
						img				=	itemMC["img"] as NxClip;
						photoID			=	StaticIni.getIniVar(String(allArr[_tabIndex][i]),"Photo");
						img.source		=	ResourcesPath.getPropsURL(int(photoID))
						obj				=	TipsMainLogic.instance.getStaticObjWithID(temp[0])//StaticIni.getIniObj(temp[0]);
						obj["weakTime"]	=	j;
						itemMC.data		=	obj;
					}
					//(i+2)为此品仙丹可以服用的最大数
					//&& isHave(allArr[_tabIndex][i])
					if(tempNum < i+2 && (medicineInfo != -1) || i < count)
					{
						itemMC			=	(_allInfoArr[i] as NxViewGroup).getItemByIndex(j);
						if(itemMC == null) continue;
//						btn				=	itemMC["btn"] as SimpleButton;	
						if(medicineInfo != -1) {
							itemMC["btnvip"].visible	=	false;
							itemMC["btn"].visible		=	true;
							itemMC["btn"]["T2"].htmlText	= String(medicineArr[medicineInfo][1]);
							itemMC["btn"].data		=	allArr[_tabIndex][i];
							itemMC["btn"].addEventListener(MouseEvent.CLICK,onUseMedicineHandle);
							itemMC["btn"].addEventListener(MouseEvent.ROLL_OVER,OnUseBtnOverHandle)
							itemMC["btn"].addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);	
						} else {
							itemMC["btn"].visible	=	false;
							itemMC["btnvip"].visible	=	true;
							itemMC["btnvip"].data		=	allArr[_tabIndex][i];
							itemMC["btnvip"].addEventListener(MouseEvent.CLICK,onUseMedicineHandle);
							itemMC["btnvip"].addEventListener(MouseEvent.ROLL_OVER,OnUseBtnOverHandle)
							itemMC["btnvip"].addEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
						}
					}			
				}
			}
			//引导时检测到界面上第一个格子有"＋"，则给予引导；没有"+"，则不给予引导
			itemMC	=	(_allInfoArr[0] as NxViewGroup).getItemByIndex(0);
			if(guildMc != null){
				_ui.removeChild(guildMc);
				guildMc = null;
			}
			if(itemMC["btn"].visible || itemMC["btnvip"].visible){
				guildMc = ResManager.newResClass("custSkin_190") as MovieClip;
				guildMc.x	= -107;
				guildMc.y	= 175;
				_ui.addChild(guildMc);
			}
			checkPack();
		}

		/**
		 * 检查丹药服用状况 
		 * 
		 */		
		private function checkPack():void
		{
			_isCanUse	=	false;
			var tempPartnerNum:int	=	0;
			var tempBool:Boolean	=	false	;
			var bool:Boolean		;
			var tempArr:Array		= 	new Array();
			var level:int			=	_model.scene.roleLevel;
			var rec:Record			=	_model.scene.GetProperties("PillUseRec");
			var arr:Array			=	rec.recArr;
			var recPartner:Record	=	_model.scene.GetProperties("PartnerPillUseRec");
			var	artnerArr:Array		=	recPartner.recArr;
			arr	=	arr.concat(artnerArr);
			//检测背包的丹药是否达到服用等级
			for each (var _arr:Array in medicineArr)
			{
				if(int(StaticIni.getIniVar(_arr[0].toString(),"NeedLevel")) <= level)
				{
					tempArr.push(_arr[0]);
				}
			}
			
			for( var i:int=0;i < tempArr.length;i++	)
			{
				tempPartnerNum	=	0
				bool	=	true;
				for( var j:int=0;j < arr.length;j++	)
				{
					if(tempArr[i] == int(arr[j][0]))
					{
						bool	=	false;
						if(StaticIni.getIniVar(tempArr[i],"MaxUse") == arr[j][1])
						{
							tempPartnerNum++;
							if(tempPartnerNum ==_partnerNum+1)
							{
								_isCanUse = false ;	
							}
							else
							{
								_isCanUse	=	true;
							}
						}
						else
						{
							_isCanUse	=	true
							return	
						}				
					}
					else
					{
						if(bool)
						{
							_isCanUse = true ;
						}			
					}
				}		
				if(_isCanUse)return
			}
		}
		
		/**
		 * 服用过的丹药
		 * @param val 查询丹药的configID
		 * @return 
		 * 有：返回丹药信息数组
		 * 否：返回空数组
		 */		
		private function queryMedicine(val:int):Array
		{
			for(var i:int =	0;i < isUseArr.length;i++)
			{
				if(val == isUseArr[i][0])
				return isUseArr[i]
			}
			return [];
		}
		
		/**
		 * 点击服用丹药
		 * @param e
		 * 
		 */		
		private function onUseMedicineHandle(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			target.gotoAndStop(3);
			var val:int	=	target.data ;
			
			if(val == 0)return
				
			//获取是哪个按钮
			var type:int = 0;
			if(target["btnType"] == 1){//物品
				type	= 1;	
			} else if(target["btnType"] == 2){//VIP
				type	= 2;
			}
			//""打开的是主角的丹药界面，否是伙伴的
			useIntensify(val, type);
		}
		
		/**
		 * 使用丹药 
		 * @param ID
		 * 
		 */		
		public function useIntensify(ID:int, btnType:int):void
		{
			
			var obj:Object	=	StaticIni.getIniObj(ID.toString());
//			var maxAdd:int	=	int(obj["AddPropertyValue"]);
			var proNameArr:Array = String(obj["AddPropertyName"]).split(",");
			var addArr:Array = String(obj["AddPropertyValue"]).split(",");
			var cutNum:int	=	int(obj["WeakenValue"]);//递减基数
			var type:int	=	int(obj["PillType"]);
			//var num:int		=	queryMedicine(ID);
//			var getNum:int	=	maxAdd;//要增加的数据
			var getNum:int	=	0;//要增加的数据
			var useNum:int;//使用丹药数量
			var name:String	=	StaticIni.getSwfLangStr(obj["NameID"]);
			var roleName:String	=	_model.scene.Role.GetProperties("Name");
			//var PartnerName:String	=	
			//var str:String	=	StaticIni.getSwfLangStrVar(strIDArr[type-1],[0]);
			var str:String;
			var getStr1:String;
			var getStr2:String;
			var _magicCoin:int = int(obj["DirectlyUseItemValue"]);
			if(_partner == "" || _partner == null)
			{
				if(_model.scene.Role.getPlayerLevel() < obj["NeedLevel"]){
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("IntensifyUIStr_11", [obj["NeedLevel"]]));
					return;
				}
				var useArr:Array	=	_model.scene.GetRecordEx("PillUseRec").recArr;
				for(var i:int = 0; i < useArr.length;i++)
				{
					if(int(useArr[i][0]) == ID)
					{
						useNum	=	useArr[i][1];
//						getNum	=	maxAdd-	cutNum*useNum;
					}	
				}
//				//丹药服满
//				if( getNum <= 0 )
//				{
//					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000513",[roleName]));
//					return
//				}
				if(addArr.length == 1){
					getStr1 = obj["AddPropertyValue"] + StaticIni.getSwfLangStr("Name_" + obj["AddPropertyName"]); //生命值
//					str	=	StaticIni.getSwfLangStr(typeStrArr[type-1]);
					if(btnType == 1){
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							//StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getNum,str]),
							StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getStr1,""]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,ID)
								}
							})	
					} else if(btnType == 2) {
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							//StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getNum,str]),
							StaticIni.getSwfLangStrVar("UIString_1000741",[_magicCoin,roleName,name,getStr1,""]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,ID)
								}
							})	
					}
				} else {
					getStr1 = addArr[0] + StaticIni.getSwfLangStr("Name_" + proNameArr[0]) + ",";
					getStr2 = addArr[1] + StaticIni.getSwfLangStr("Name_" + proNameArr[1]);
//					str	=	StaticIni.getSwfLangStr(typeStrArr[type-1]);
					if(btnType == 1){
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getStr1,getStr2]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,ID)
								}
							})	
					} else if(btnType == 2) {
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							StaticIni.getSwfLangStrVar("UIString_1000741",[_magicCoin,roleName,name,getStr1,getStr2]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PLAYER_USE,ID)
								}
							})	
					}
				}
				
			}
			else
			{
				if(_partnerLevel < obj["NeedLevel"]){
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("IntensifyUIStr_11", [obj["NeedLevel"]]));
					return;
				}
				var partarr:Array	=	_model.scene.GetRecordEx("PartnerPillUseRec").recArr;
				var tempArr:Array	=	getPartnerInfo(partarr);
				for(var j:int = 0; j <tempArr.length;j++)
				{
					if(tempArr[j][0] == ID)
					{
						useNum	=	tempArr[j][1];
//						getNum	=	maxAdd-	cutNum*useNum;
					}
				}
//				//丹药服满
//				if( getNum <= 0 )
//				{
//					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000513",[_partnerNAME]));
//					return
//				}
				if(addArr.length == 1){
					getStr1 = obj["AddPropertyValue"] + StaticIni.getSwfLangStr("Name_" + obj["AddPropertyName"]); //生命值
//					str	=	StaticIni.getSwfLangStr(typeStrArr[type-1]);
					if(btnType == 1){
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你的伙伴该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							//StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getNum,str]),
							StaticIni.getSwfLangStrVar("UIString_1000453",[_partnerNAME,name,getStr1,""]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PARTNER_USE,ID,int(_partner));
								}
							})	
					} else if(btnType == 2) {
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你的伙伴该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							//StaticIni.getSwfLangStrVar("UIString_1000453",[roleName,name,getNum,str]),
							StaticIni.getSwfLangStrVar("UIString_1000741",[_magicCoin,_partnerNAME,name,getStr1,""]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PARTNER_USE,ID,int(_partner));
								}
							})	
					}
					
				} else {
					getStr1 = addArr[0] + StaticIni.getSwfLangStr("Name_" + proNameArr[0]) + ",";
					getStr2 = addArr[1] + StaticIni.getSwfLangStr("Name_" + proNameArr[1]);
//					str	=	StaticIni.getSwfLangStr(typeStrArr[type-1]);
					if(btnType == 1){
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你的伙伴该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							StaticIni.getSwfLangStrVar("UIString_1000453",[_partnerNAME,name,getStr1,getStr2]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PARTNER_USE,ID,int(_partner));
								}
							})	
					} else if(btnType == 2) {
						AlertUI.showAlert(/*StaticIni.getSwfLangStr("喂!你的伙伴该吃药了")*/StaticIni.getSwfLangStr("MessageUI_Notice"),
							StaticIni.getSwfLangStrVar("UIString_1000741",[_magicCoin,_partnerNAME,name,getStr1,getStr2]),
							AlertUI.YES|AlertUI.NO,
							function(type:int, str:String, clickData:*):void
							{
								if (type	==	AlertUI.YES)
								{
									SystemSender.getInstance().
									CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PILL_PARTNER_USE,ID,int(_partner));
								}
							})	
					}
				}
			}
			
			_model.dispatchEvent(new SoundEvent(SoundEvent.SWALLOW_MEDICINE));
			LogSendLogic.getInstance().btnClickNumSend(22803);	
		}
			
				
		private function clickFunc(clickName:uint,inputTxt:String,clickData:Array):void
		{
			if(clickName==AlertUI.OK)
			{
				AlertUI.hideAlert();
			}
		}
		
		
		
		public function updataByBuddy(view:ObjectData):void
		{
			setObj( view );
//			
			AfterHide();
			BeforeShow();
//			unBind();	
//			groupInfo();
		}
			
		/**
		 * 获得当前所选伙伴的ID
		 * @param partnerID
		 * 
		 */		
		public function  setObj(view:ObjectData):void
		{
			if(_model.scene.Role == view)
			{
				_partner	=	"";
			}
			else
			{
				_partner	=	view.GetProperties("ConfigID");
				_partnerLevel	=	view.GetProperties("Level");
				_partnerNAME	=	StaticIni.getSwfLangStr(_partner);
			}
			_partnerView	=	view;		
		}
		
		/**
		 *清空表格 
		 * 数据
		 */		
		private function clearGroupInfo():void
		{
			var img 	: NxClip;
			var itemMC 	: MovieClip;
			var btn  	: SimpleButton;
			var obj		: Object	=	new Object();
			obj["weakTime"]			=	-1;
			for( var i:int = 0 ; i <  _allInfoArr.length; i++ )
			{
				var tempNum:int	=	(_allInfoArr[i] as NxViewGroup).row
				for(var j:int = 0 ; j <  tempNum; j++)
				{
					itemMC			=	(_allInfoArr[i] as NxViewGroup).getItemByIndex(j);
					itemMC.buttonMode		=	false;
					itemMC.data		=	obj;
//					img 			= 	itemMC.getChildByName("img") as NxClip;
					img				=	itemMC["img"] as NxClip;
					itemMC["btn"].visible	=	false;
					itemMC["btnvip"].visible	=	false;
					if(img == null)continue;
					img.source		=	"";
					//itemMC.removeEventListener(MouseEvent.ROLL_OVER,onMouseOverHandle);
					itemMC["btn"].removeEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
					itemMC["btn"].removeEventListener(MouseEvent.ROLL_OVER,onMouseOverHandle);
					itemMC["btnvip"].removeEventListener(MouseEvent.ROLL_OUT,onMouseOutHandle);
					itemMC["btnvip"].removeEventListener(MouseEvent.ROLL_OVER,onMouseOverHandle);
				}		
			}
		}
		private function onClickClose(e:Event):void
		{
			_ui.hide();
		}
		//		/**
		//		 * 对表格数据按种类储存
		//		 * @param arr
		//		 * 
		//		 */		
		//		private function sortData(arr:Array):void
		//		{
		//			for(var i:int = 0;i < arr.length;i++)
		//			{
		//				switch(arr[i][2])
		//				{
		//					case 0:
		//						isUseArr[0].push(arr[i]);
		//						break;	
		//					case 1:
		//						isUseArr[1].push(arr[i]);
		//						break;	
		//					case 2:
		//						isUseArr[2].push(arr[i]);
		//						break;	
		//				}
		//			}
		//		}
		
		/**
		 * 背包中是否有可以使用的丹药
		 * @return 
		 * 
		 */		
		public function get isCanUse():Boolean
		{
			
			return _isCanUse
		}
		
	}
}